package com.hxh.juc;

import java.util.concurrent.CountDownLatch;

/**
 * @author 兴趣使然黄小黄
 * @version 1.0
 * @date 2023/9/10 21:36
 * CountDownLatch Demo
 * 六个同学陆续离开教室后班长锁门
 */
public class CountDownLatchDemo {

    public static void main(String[] args) throws InterruptedException {
        // 创建 CountDownLatch 对象, 设置初始值
        CountDownLatch countDownLatch = new CountDownLatch(6);
        // 6 个同学依次出教室
        for (int i = 1; i <= 6; i++) {
            new Thread(() -> {
                System.out.println(Thread.currentThread().getName() + " 号同学离开了教室");
                countDownLatch.countDown(); // 计数 -1
            }, String.valueOf(i)).start();
        }
        countDownLatch.await(); // 等待
        System.out.println(Thread.currentThread().getName() + " 班长锁门");
    }
}
